package com.example.yyy

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Build
import androidx.core.app.NotificationCompat
import org.jetbrains.anko.intentFor

class SendCounterNotify (context: Context, title: String, message: String,progress:Int) {
    private companion object val CHANNELID: String = "com.example.yyy"
    init {

        //声明一个点击通知消息时触发的动作意图
        val clickIntent = context.intentFor<MainActivity>()
        val piDeleted = PendingIntent.getActivity(
            context,
            R.string.app_name,
            clickIntent,
            PendingIntent.FLAG_CANCEL_CURRENT
        )
        //构建消息参数
        val builder: NotificationCompat.Builder = NotificationCompat.Builder(context, CHANNELID)
        builder.setDeleteIntent(piDeleted)
            .setAutoCancel(true)
            .setUsesChronometer(true)
            .setProgress(100,progress,false)
            .setNumber(99)
            .setSubText("进度条")
            .setTicker("计数消息")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setWhen(System.currentTimeMillis())
            .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.tp))
            .setContentTitle(title)
            .setContentText(message)
        //获取通知管理器
        val manager: NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //andriod Q 之后需要一个渠道
            val channel = NotificationChannel("2", "channel2", NotificationManager.IMPORTANCE_DEFAULT)
            channel.enableLights(true)
            channel.lightColor = Color.green(1)
            channel.setShowBadge(true)
            manager.createNotificationChannel(channel)
            builder.setChannelId("2")
        }
        val n = builder.build()
        manager.notify(2, n)
    }
}